package com.liuceng.zen.lib.gdt.clients;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.github.kevinsawicki.http.HttpRequest;
import com.liuceng.zen.lib.gdt.GdtClient;
import com.liuceng.zen.lib.gdt.util.GdtUtils;
import com.liuceng.zen.lib.gdt.vo.ApiBusinessInterest;
import com.liuceng.zen.lib.gdt.vo.ApiIndustryConf;
import com.liuceng.zen.lib.gdt.vo.ApiUtilities;
import com.liuceng.zen.lib.gdt.vo.ApiUtilitiesLocation;
import com.liuceng.zen.lib.gdt.vo.GdtList;
import com.liuceng.zen.lib.gdt.vo.GdtResult;
import com.liuceng.zen.lib.gdt.vo.other.ListJson;

@Service
public class GdtUtilitiesClient extends GdtClient<ApiBusinessInterest> {

  public final static String BUSINESS_INTEREST = "business_interest";
  public final static String AREA_LIST = "area_list";
  public final static String CITY_LIST = "city_list";
  public final static String LOCATION_LIST = "location_list";
  public final static String GET_INDUSTRY_CONF = "get_industry_conf";
  public final static String GET_ADVERTISER_INDUSTRY_CONF = "get_advertiser_industry_conf";

  public GdtResult<GdtList<ApiBusinessInterest>> businessInterest() {
    return super.selectUtilities(ApiUtilities.RES_NAME, BUSINESS_INTEREST,
        new TypeReference<GdtResult<GdtList<ApiBusinessInterest>>>() {});
  }

  public GdtResult<ListJson> cityList() {
    String url = GdtUtils.buildUrl(apiUrl, ApiUtilities.RES_NAME, CITY_LIST);
    Map<String, Object> params = new HashMap<>();
    params.put("token", GdtUtils.buildToken(Integer.parseInt(appid), appid, appkey));
    HttpRequest req = HttpRequest.get(url, true, map2arr(params));
    if (200 != req.code()) {
      LOG.error("cityList error:" + req.code() + "\r\n body:" + req.body());
      return new GdtResult<ListJson>(-1, "network error!");
    }
    String content = req.body();
    GdtResult<ListJson> res =
        JSON.parseObject(content, new TypeReference<GdtResult<ListJson>>() {});
    return res;
  }

  public GdtResult<GdtList<ApiUtilities>> areaList() {
    String url = GdtUtils.buildUrl(apiUrl, ApiUtilities.RES_NAME, AREA_LIST);
    Map<String, Object> params = new HashMap<>();
    params.put("token", GdtUtils.buildToken(Integer.parseInt(appid), appid, appkey));
    HttpRequest req = HttpRequest.get(url, true, map2arr(params));
    if (200 != req.code()) {
      LOG.error("areaList error:" + req.code() + "\r\n body:" + req.body());
      return new GdtResult<GdtList<ApiUtilities>>(-1, "network error!");
    }
    String content = req.body();
    GdtResult<GdtList<ApiUtilities>> res =
        JSON.parseObject(content, new TypeReference<GdtResult<GdtList<ApiUtilities>>>() {});
    return res;
  }

  public GdtResult<ApiUtilitiesLocation> locationList(Integer id) {
    String url = GdtUtils.buildUrl(apiUrl, ApiUtilities.RES_NAME, LOCATION_LIST);
    Map<String, Object> params = new HashMap<>();
    params.put("token", GdtUtils.buildToken(Integer.parseInt(appid), appid, appkey));
    params.put("cityid", id);
    HttpRequest req = HttpRequest.get(url, true, map2arr(params));
    if (200 != req.code()) {
      LOG.error("locationList error:" + req.code() + "\r\n body:" + req.body());
      return new GdtResult<ApiUtilitiesLocation>(-1, "network error!");
    }
    String content = req.body();
    GdtResult<ApiUtilitiesLocation> res =
        JSON.parseObject(content, new TypeReference<GdtResult<ApiUtilitiesLocation>>() {});
    return res;
  }

  public GdtResult<GdtList<ApiIndustryConf>> industryConf() {
    String url = GdtUtils.buildUrl(apiUrl, ApiUtilities.RES_NAME, GET_INDUSTRY_CONF);
    Map<String, Object> params = new HashMap<>();
    params.put("token", GdtUtils.buildToken(Integer.parseInt(appid), appid, appkey));
    HttpRequest req = HttpRequest.get(url, true, map2arr(params));
    if (200 != req.code()) {
      LOG.error("industryConf error:" + req.code() + "\r\n body:" + req.body());
      return new GdtResult<GdtList<ApiIndustryConf>>(-1, "network error!");
    }
    String content = req.body();
    GdtResult<GdtList<ApiIndustryConf>> res =
        JSON.parseObject(content, new TypeReference<GdtResult<GdtList<ApiIndustryConf>>>() {});
    return res;
  }

  public GdtResult<GdtList<ApiIndustryConf>> adIndustryConf() {
    String url = GdtUtils.buildUrl(apiUrl, ApiUtilities.RES_NAME, GET_ADVERTISER_INDUSTRY_CONF);
    Map<String, Object> params = new HashMap<>();
    params.put("token", GdtUtils.buildToken(Integer.parseInt(appid), appid, appkey));
    HttpRequest req = HttpRequest.get(url, true, map2arr(params));
    if (200 != req.code()) {
      LOG.error("advertiserIndustryConf error:" + req.code() + "\r\n body:" + req.body());
      return new GdtResult<GdtList<ApiIndustryConf>>(-1, "network error!");
    }
    String content = req.body();
    GdtResult<GdtList<ApiIndustryConf>> res =
        JSON.parseObject(content, new TypeReference<GdtResult<GdtList<ApiIndustryConf>>>() {});
    return res;
  }
}
